import { LoadMaskService } from './primeng/loadmask/loadmask.module';

/**
 * 添加加载蒙层
 */
export function loadingMask() {
    return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
        let method = descriptor.value;
        descriptor.value = function () {
            LoadMaskService.Current.show();
            try {
                let that = this;
                let args = arguments;
                var promise = new Promise(function (resolve, reject) {
                    resolve(method.apply(that, args));
                });
                promise.then(function () {
                    LoadMaskService.Current.hide();
                }).catch(function () {
                    LoadMaskService.Current.hide();
                })
            } catch (error) {
                LoadMaskService.Current.hide();
            } finally {

            }
        }
    }
}
/**
 * 防止重复点击，300毫秒延迟
 */
export function aovidDoubleClick() {
    return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
        let method = descriptor.value;
        let timeObj: any = {};
        descriptor.value = function () {
            if (!timeObj[propertyKey]) {
                timeObj[propertyKey] = new Date().getTime();
            } else {
                let oldTime = timeObj[propertyKey];
                let newTime = new Date().getTime();
                if (newTime - oldTime < 300) {
                    return;
                }
            }
            timeObj[propertyKey] = new Date().getTime();
            method.apply(this, arguments);
        }
    }
}